package com.csdj.t309.papermanage.controller;

import com.csdj.t309.papermanage.common.WebResult;
import com.csdj.t309.papermanage.entity.TPaper;
import com.csdj.t309.papermanage.entity.TPaperExample;
import com.csdj.t309.papermanage.entity.TType;
import com.csdj.t309.papermanage.service.TpaperService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@Controller
@RequestMapping("/tpaper")
public class TpaperController {
    @Resource
    private TpaperService tpaperService;
    @ResponseBody
    @GetMapping("/chadx")
    public List<TPaper> delTp(@RequestParam(value = "lei",required = false) String lei, @RequestParam(value = "type",required = false) String type){
        TPaperExample tPaperExample=new TPaperExample();
        tPaperExample.createCriteria().andTypeCodeEqualTo(type).andFileNameEqualTo(lei);
        return tpaperService.selectByExample(tPaperExample);
    }

    @ResponseBody
    @GetMapping("/seltps")
    public WebResult seltps(@RequestParam(required = true, defaultValue = "1") Integer page){
        PageHelper.startPage(page,5);
        List<TPaper> posts = tpaperService.selTps();
        PageInfo<TPaper> p = new PageInfo<>(posts);
        WebResult webResult = new WebResult();
        webResult.putData("posts",posts).putData("p",p);
        return webResult;
    }

    @ResponseBody
    @GetMapping("/del")
    public WebResult delTp(@RequestParam(value = "id",required = false) Integer id){
        Integer result = tpaperService.delTp(id);
        if(result>0){
            WebResult  webResult = new WebResult();
            webResult.putData("result",result);
            return webResult;
        }
        return null;
    }

    @ResponseBody
    @RequestMapping("positionUpload")
    public String companyUpload1(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {
       TPaper tPaper=new TPaper();
        String filename = file.getOriginalFilename();
        System.out.println(filename);
        String ext = filename.substring(filename.lastIndexOf("."));
        System.out.println(filename);
        long curTime = System.currentTimeMillis();
        try {
            Path path = Paths.get("d:", "upload");
            if (!Files.exists(path)) {
                Files.createDirectories(path);
            }
            file.transferTo(Paths.get("d:", "NZrengli/img", String.valueOf(curTime) + ext));
            tPaper.setPaperPath(String.valueOf(curTime) + ext);
        } catch (IOException e) {
            e.printStackTrace();
        }
        tPaper.setTitle(request.getParameter("title"));
        tPaper.setTypeCode(request.getParameter("type"));
        tPaper.setPaperSummary(request.getParameter("zy"));
        if (tpaperService.insertTp(tPaper) > 0) {
            return "true";
        } else {
            return "false";
        }

    }
}
